
import { Component, OnInit, ElementRef, AfterViewInit, ViewChild } from '@angular/core';
import { Http, Response } from '@angular/http';
import { DxDataGridComponent } from 'devextreme-angular';
import 'rxjs/add/operator/map';
import { Subject } from 'rxjs/Rx';
import { Observable } from 'rxjs/Rx';
import { CommService } from '../../comm/comm.service';
import { ReportUrls } from '../../comm/report.urls.conf';
import { CTS } from '../../model/report/cts';
import { CTSChart } from '../../model/report/cts-chart';
import { SubArea } from '../../model/report/sub-area';
import { Resource } from '../../model/report/resource';
import { QueryModel } from '../../model/report/query-model';
import { ResponseModel } from '../../model/comm/response-model';
import { ChartOptionCreater } from '../../model/comm/chart-option-creater';
import { Shift } from '../../model/report/shift';
import 'rxjs/add/operator/toPromise';
declare const $: any;
import * as moment from 'moment';
declare var swal: any;
import * as Enumerable from 'linq';
import { TVResource } from 'app/ptpmc/model/comm/tv-resource';
import { DxDropDownBoxComponent } from 'devextreme-angular';
import { GlobalDataManager } from 'app/ptpmc/comm/global-data-manager';

@Component({
    selector: 'app-report-ctd',
    templateUrl: './cyclertime.component.html',
    styleUrls: ['./cyclertime.component.scss']
})

export class CyclerTimeComponent implements OnInit {
    /*生产线 - 班次*/
    public subAreaItems: SubArea[];
    public shiftItems: any[];
    public equipemntItems: TVResource[] = [];
    public subAareSelected: any;
    public shiftSelected: any;
    public equipmentSelected: TVResource;
    title = 'CT明细';
    public ctsList: CTS[];
    public showUpload: boolean = false;

    constructor(private commServices: CommService) {
    }

    ngOnInit() {
        this.commServices.initDatePicker($('#DateTimeRange'), 'YYYY-MM-DD HH:mm');
        this.shiftItems = this.commServices.shifts;
        this.shiftSelected = this.shiftItems[0];
        this.subAreaItems = this.commServices.getSubAreas();
        this.subAareSelected = this.subAreaItems[0];

        this.equipemntItems = this.commServices.getTVResourceBySubArea(this.subAreaItems[0].subArea);
        this.equipmentSelected = this.equipemntItems[0];
        this.showUpload = GlobalDataManager.getProjectSelected().projectName === 'PHEV' || GlobalDataManager.getProjectSelected().projectName === 'UBEV';
    }
    onSubAreaSelectedChange(subArea: any): void {
        this.equipemntItems = this.commServices.getTVResourceBySubArea(subArea.value.subArea);
        this.equipmentSelected = this.equipemntItems[0];
    }
    /**
     * 获取表格数据
     */
    getReportData(): void {
        const queryModel: QueryModel = new QueryModel();
        queryModel.dateTimeRange = this.getTime('DateTimeRange');
        queryModel.querySubAreaIds = [this.subAareSelected.subArea];
        queryModel.queryResourceIds = [this.equipmentSelected.resource];
        queryModel.shift = this.shiftSelected.shiftId;
        this.commServices
            .getList(queryModel, ReportUrls.CTListUrl)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    if(queryModel.querySubAreaIds[0].slice(0,2) === 'GF'){
                        model.data.forEach(e => {
                            var sub: string = '';
                            sub = e.subArea.replace(/(^\s*)|(\s*$)/g, "");
                            switch(sub){
                                case 'GFAL1': sub += '_装配主线';  break;
                                case 'GFBU1': sub += '_测试线';  break;
                                case 'GFCL1': sub += '_CASE 机加工线';  break;
                                case 'GFCR1': sub += '_行星齿轮组分装线';  break;
                                case 'GFCS1': sub += '_中间支撑架分装线';  break;
                                case 'GFDH1': sub += '_飞轮盘减震器壳体分装线';  break;
                                case 'GFHL1': sub += '_HOUSING 机加工线';  break;
                                case 'GFTC1': sub += '_壳体分装线';  break;
                                case 'GFTD1': sub += '_中间轴&差速器分装线';  break;
                            }
                            e.subArea = sub;
                        });
                    }
                    this.ctsList = model.data;
                } else {
                    console.log(model.resultCode + ',' + model.resultMessage);
                }
            });
    }

    getTime(id: any): String {
        const timerange: String = $('#' + id).val();
        return timerange;
    }

    onExporting(e) {
        e.fileName = e.fileName + new Date().toISOString().replace(/[\-\:\.]/g, '').split('T')[0];
    }
}
